package core.persistence;

import java.io.InputStream;

import br.com.simple.jdbc.database.IDatabase;
import br.com.simple.jdbc.setup.SqliteSetupHelper;
import br.com.simple.jdbc.setup.StartupConfig.PoolConfig;
import br.com.simple.jdbc.util.SqlTool;

public class SqliteRepositoryStartup extends SqliteSetupHelper{

    private static final int 	DATABASE_VERSION 		= 1;
    private static final String DATABASE_NAME 	        = "openp2m.sqlite";

    private String ddlPath;
    
    public SqliteRepositoryStartup(String dbPath,String ddlPath) {
    	this(dbPath,ddlPath,null);
    }
    
    public SqliteRepositoryStartup(String dbPath,String ddlPath,PoolConfig poolConfig) {
    	super(dbPath + "/" + DATABASE_NAME,DATABASE_VERSION,poolConfig);
    	this.ddlPath = ddlPath;
	}
    
	@Override
	public void onCreate(IDatabase db) {
		String filePath = ddlPath + "/" + DATABASE_NAME + ".ddl.sql";
		InputStream in = this.getClass().getResourceAsStream(filePath);
		try {
			String ddl = SqlTool.mysql2SqliteDDL(readInputStreamToString(in));
			createFromDDL(db,ddl);
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	protected void buildPrimaryData(IDatabase db) {		
	}

	@Override
	public void onUpgrade(IDatabase database, int oldVersion, int newVersion) {
	}

	@Override
	public void onOpen(IDatabase database) {
	}	


}
